package com.asiainfo.dacp.datastash.domain.stepdefine.repos;

import com.asiainfo.dacp.datastash.domain.stepdefine.models.DataStashStepDefine;
import com.asiainfo.dacp.datastash.orm.dao.core.DataStashStepDefineDao;
import com.asiainfo.dacp.datastash.orm.po.core.DataStashStepDefinePo;
import com.asiainfo.dacp.datastash.orm.po.state.DataStashUnitTaskStateLogPo;
import com.asiainfo.dacp.util.BeanConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Repository;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created by winter on 2017/10/31.
 */
@Repository
public class DataStashStepDefineRepository {
    @Autowired
    private DataStashStepDefineDao dataStashStepDefineDao;

    public List<DataStashStepDefine> findAll() {
        return BeanConvertUtils.copyList(dataStashStepDefineDao.findAll(), DataStashStepDefine.class);
    }

    public DataStashStepDefine findOne(String id) {
        return BeanConvertUtils.copyBean(dataStashStepDefineDao.findOne(id), DataStashStepDefine.class);
    }

    public void deleteEntity(String id)throws Exception{
        dataStashStepDefineDao.delete(id);
    }
    public DataStashStepDefine save(DataStashStepDefine define)throws Exception{
        dataStashStepDefineDao.save(BeanConvertUtils.copyBean(define, DataStashStepDefinePo.class));
        return define;
    }
    public Page<DataStashStepDefine> findAll(int pageNum, int pageSize, final String stepInstLabel,
                                             final String stepType, final String groupType, final String stepState,
                                             final String stepStyle, final String sortColumn, final String desc) throws Exception {
        //TODO 增加按创建时间逆序排序
        Pageable pageable = new PageRequest(pageNum, pageSize);
        //TODO 获取对象应该是一个包含字段的丰满对象
        Page<DataStashStepDefinePo> tablePage = dataStashStepDefineDao.findAll(new Specification<DataStashStepDefinePo>() {
            public Predicate toPredicate(Root<DataStashStepDefinePo> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                //TODO 增加过滤条件
                List<Predicate> predicate = new ArrayList<Predicate>();
                Predicate[] p = new Predicate[predicate.size()];
                if (!"all".equals(stepInstLabel)) {
                    predicate.add(cb.like(root.get("stepInstLabel").as(String.class), "%" + stepInstLabel + "%"));
                }
                if (!"all".equals(stepType)) {
                    predicate.add(cb.equal(root.get("stepType").as(String.class), stepType));
                }
                if (!"all".equals(groupType)) {
                    predicate.add(cb.equal(root.get("groupType").as(String.class), groupType));
                }
                if (!"all".equals(stepState)) {
                    predicate.add(cb.equal(root.get("stepState").as(String.class), stepState));
                }
                if (!"all".equals(stepStyle)) {
                    predicate.add(cb.equal(root.get("stepStyle").as(String.class), stepStyle));
                }
                if ("desc".equals(desc)) {
                    query.orderBy(cb.desc(root.get(sortColumn).as(
                            String.class)));
                } else if ("asc".equals(desc)) {
                    query.orderBy(cb.asc(root.get(sortColumn).as(
                            String.class)));
                }
                query.where(cb.and(predicate.toArray(p)));
                return query.getRestriction();
            }
        }, pageable);
        List<DataStashStepDefine> list = new ArrayList<DataStashStepDefine>();
        Iterator<DataStashStepDefinePo> iter = tablePage.iterator();
        while (iter.hasNext()) {
            DataStashStepDefinePo po = iter.next();
            DataStashStepDefine vo = BeanConvertUtils.copyBean(po, DataStashStepDefine.class);
            list.add(vo);
        }
        Page<DataStashStepDefine> page = new PageImpl<DataStashStepDefine>(list, pageable, tablePage.getTotalElements());
        return page;
    }

}
